package util;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtil {
	public static Connection getPGConnection(String url, String user, String passwd) throws SQLException, ClassNotFoundException {
		Class.forName("org.postgresql.Driver");
		Connection con = DriverManager.getConnection
		//("jdbc:postgresql://nmssql:5432/pg_utf8", "postgres", "adminpasswd");
		(url, user, passwd);
		return con;
	}
	public static Connection getOracleConnection(String url, String user, String passwd) throws SQLException, ClassNotFoundException {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection con = DriverManager.getConnection( url, user, passwd);
		return con;
	}
	public static Connection getConnection(String url, String user, String passwd) throws SQLException, ClassNotFoundException {
		if (url.toLowerCase().contains("oracle")) {
			return getOracleConnection(url, user, passwd);
		} else if (url.toLowerCase().contains("postgres")) {
			return getPGConnection(url, user, passwd);
		} else {
			throw new IllegalArgumentException("Unknown DB Type:" + url);
		}
	}
	public static void show(ResultSet rs) throws SQLException {
		show(System.out, rs);
	}
	public static void show(PrintStream os, ResultSet rs) throws SQLException {
		ResultSetMetaData meta = rs.getMetaData();
		int cols = meta.getColumnCount();
		String[] colNames = new String[cols];
		for (int i=0 ; i<cols ; i++) {
			colNames[i] = meta.getColumnLabel(i+1);
		}
		while (rs.next()) {
			for (int i=0 ; i<cols ; i++) {
				os.printf("%s:'%s' ", colNames[i], rs.getString(i+1));
			}
			os.println();
		}
	}
	public static class Resource {
		private Statement stmt;
		private ResultSet rs;
		public Resource(Statement stmt, ResultSet rs) {
			this.stmt = stmt;
			this.rs = rs;
		}
		public Resource(Statement stmt) {
			this(stmt, null);
		}
		public Resource(ResultSet rs) {
			this(null,rs);
		}
		public Statement getStatement() {
			return stmt;
		}
		public ResultSet getResultSet() {
			return rs;
		}
		public void close() throws SQLException {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
	}
	public static String getSingleString(Connection conn, String sql) throws SQLException {
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		String result = null;
		if (rs.next()) {
			result = rs.getString(1);
		}
		rs.close();
		stmt.close();
		return result;
	}
    public static Long getSingleLong(Connection conn, String sql) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        Long result = null;
        if (rs.next()) {
            result = rs.getLong(1);
        }
        rs.close();
        stmt.close();
        return result;
    }
    abstract public static class Traverser {
    	public Traverser(Connection conn, String sql) throws SQLException {
    		Statement stmt = conn.createStatement();
    		ResultSet rs = stmt.executeQuery(sql);
    		prepare(rs);
    		while (rs.next()) {
    			visit(rs);
    		}
    		rs.close();
    		stmt.close();
    	}
		protected void prepare(ResultSet rs) throws SQLException {}
		protected abstract void visit(ResultSet rs) throws SQLException;
    }
}
